package com.trimps.wifi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.trimps.wifi.common.Constants;
import com.trimps.wifi.common.Paginator;
import com.trimps.wifi.exception.LoginException;
import com.trimps.wifi.exception.SqlOperationsException;
import com.trimps.wifi.pojo.BlacklistInfoData;
import com.trimps.wifi.pojo.FlowData;
import com.trimps.wifi.pojo.User;
import com.trimps.wifi.service.BlacklistService;
import com.trimps.wifi.service.IUserService;

@Controller
@RequestMapping(value = "alarm/")
public class BlacklistController extends BaseController {
    private static final Logger logger = Logger.getLogger(BlacklistController.class);

    @Autowired
    private BlacklistService blacklistService;

    @RequestMapping(value = "blacklist")
    public String blacklistPage() {
    	return "alarm/blacklist";
    }

    /**
     * 获取黑名单列表
     * @param 
     * @return
     */
    @RequestMapping(value = "blacklistInfo", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> blacklistInfoList(@RequestParam(value="curPage", required = false) String cp, @RequestParam(value="totalPage", required = false) String tp,
                    @RequestParam(value="keywords", required = false) String keywords) {
        // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        int curPage = Constants.DEFAULT_FIRST_PAGE, totalCount, totalPage;
        List<BlacklistInfoData> lst = new ArrayList<BlacklistInfoData>();
        // if ("".equals(tp)) {
        totalCount = blacklistService.getBlacklistCountByKeyword(keywords);
        if (totalCount == 0) {
            totalPage = 0;
        } else {
            // 计算totalpage
            totalPage = (int) Math.ceil((double) totalCount / (double) Constants.RECORD_PER_PAGE);
        }

        if (totalPage != 0) {
            if (!("".equals(cp))) {
                curPage = Integer.parseInt(cp);
            }
            lst = blacklistService.getBlacklistByKeyword(keywords, curPage, Constants.RECORD_PER_PAGE);
        }

        map.put("totalPage", totalPage);
        map.put("currentPage", curPage);
        map.put("listInfo", lst);
        map.put("totalCount", totalCount);
        return map;
    }
    
    @RequestMapping(value = "getBlacklistById", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getBlacklistById(@RequestParam(value="id", required=true) int id) {
        // 返回用
        Map<String, Object> map = new HashMap<String, Object>();
        BlacklistInfoData blacklistInfoData = blacklistService.getBlacklistById(id);

        map.put("dataInfo", blacklistInfoData);
        return map;
    }
    
    @RequestMapping(value = "addBlacklist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addBlacklist(BlacklistInfoData blacklistInfoData,ModelMap modelMap) {
        int id = blacklistService.addBlacklist(blacklistInfoData);
        if(id > 0){
            return super.buildSuccessMsg("添加成功！");
        }
        
        return super.buildFailureMsg("添加失败！");
    }
    
    
    @RequestMapping(value = "updateBlacklist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateBlacklist(BlacklistInfoData blacklistInfoData,ModelMap modelMap) {
        int ret = blacklistService.updateBlacklist(blacklistInfoData);
        if(ret > 0){
            return super.buildSuccessMsg("修改成功！");
        } 
        return super.buildFailureMsg("修改失败！");
    }
    
    @RequestMapping(value = "delBlacklist", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> delBlacklist(@RequestParam(value="id", required=true) int id,ModelMap modelMap) {
        int ret = blacklistService.delBlacklistById(id);
        if(ret > 0){
            return super.buildSuccessMsg("删除成功！");
        }
        return super.buildFailureMsg("删除失败！");
    }

}
